﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class BlogCategoryBll:IBlogCategoryBll
    {
        private IBlogCategoryDal _dal;

        public BlogCategoryBll(IBlogCategoryDal dal)
        {
            _dal = dal;
        }

        public async Task<int> AddBlogCategoryAsync(int Bid, int Cid)
        {
            return await _dal.AddModelAsync(new BlogCategory
            {
                bid = Bid,
                cid = Cid
            });
        }

        public async Task<int> DeleteBlogCategoryAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditBlogCategoryAsync(int Id, int Bid, int Cid)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.bid = Bid;
            data.cid = Cid;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<BlogCategoryDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(x => new BlogCategoryDtos
            {
                id = x.Id,
                bid = x.bid,
                cid = x.cid,
               
            }) : new List<BlogCategoryDtos>();
        }

        public async Task<BlogCategoryDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new BlogCategoryDtos() : new BlogCategoryDtos
            {
                id = data.Id,
                bid = data.bid,
                cid = data.cid,
              
            };
        }
    }
}
